package com.onpositive.realmview.search;

import java.util.ArrayList;
import java.util.HashSet;
import com.onpositive.realmview.model.IArtifact;
import com.onpositive.semantic.model.realm.ISimpleChangeListener;

public final class GlobalSearch {

	static HashSet<ISimpleChangeListener<String>> listeners = new HashSet<ISimpleChangeListener<String>>();
	static HashSet<ISimpleChangeListener<String>> preSetListeners = new HashSet<ISimpleChangeListener<String>>();
	
	public static void addPreSetListener(ISimpleChangeListener<String> ls) {
		preSetListeners.add(ls);
	}
	
	public static void removePreSetListener(ISimpleChangeListener<String> ls) {
		preSetListeners.remove(ls);
	}

	public static void addListener(ISimpleChangeListener<String> ls) {
		listeners.add(ls);
	}

	public static void removeListener(ISimpleChangeListener<String> ls) {
		listeners.remove(ls);
	}

	private GlobalSearch() {

	}

	private static String currentPattern;

	public static void setPattern(String pattern) {
		for (ISimpleChangeListener<String> m : preSetListeners) {
			m.changed(pattern, null);
		}
		if (pattern == "") {
			pattern = null;
		} else {
			pattern = pattern.toLowerCase();
		}
		if (pattern == null && currentPattern == null) {
			return;
		} else if (pattern != null && pattern.equals(currentPattern)) {
			return;
		}
		currentPattern = pattern;
		for (ISimpleChangeListener<String> m : listeners) {
			m.changed(currentPattern, null);
		}
	}

	public static int[] getMatches(String text) {
		if (text == null || currentPattern == null || currentPattern.length() == 0) {
			return null;
		}
		int pos = 0;
		ArrayList<Integer> rr = new ArrayList<Integer>();
		int i = 0;
		while (true) {
			int indexOf = text.toLowerCase().indexOf(currentPattern, pos);
			if (indexOf != -1) {				
				rr.add(indexOf);
				pos = indexOf + currentPattern.length();
				//System.err.println("Loop iteration " + i + " indexOf=" + indexOf+" pos= "+pos+" ** Total Memory: " + r.totalMemory() + " Max Memory: " + r.maxMemory() + " Free Memory: " + r.freeMemory() );
				rr.add(pos);
			} else {
				break;
			}
			i++;
			if( i > 5){
				break;
			}
		}
		int[] matches = new int[rr.size()];
		for (int a = 0; a < rr.size(); a++) {
			matches[a] = rr.get(a);
		}
		return matches;
	}

	public static String getPattern() {
		return currentPattern == null ? "" : currentPattern;
	}

	public static boolean accept(IArtifact c) {
		if (currentPattern == null) {
			return true;
		}
		
		String title = c.getTitle();
		title = title.toLowerCase();
		if (title.indexOf(currentPattern) != -1) {
			return true;
		}
		return false;
	}

//	public static Collection<Term> getSearchTerms(String contents) {
//		HashSet<Term>m=new HashSet<Term>();
//		for (IService s:ServicesRealmProvider.getInstance().getServices()){
//			ISearchTermsProvider adapter = s.getAdapter(ISearchTermsProvider.class);
//			if (adapter!=null){
//				m.addAll(adapter.getTerms(contents));
//			}
//		}
//		return m;
//	}
}
